package com.holystone.splunk.app.model;

import com.holystone.splunk.app.enums.AlertEnums;
import com.holystone.splunk.app.mybatis.model.Alert;
import com.holystone.splunk.app.mybatis.model.Event;
import com.holystone.splunk.app.utils.AlertUtils;
import com.holystone.splunk.utils.DateFormatUtils;

import java.util.*;

/**
 * Copyright (c) 2017, HOLYSTONE Technologies, Inc.
 * All right reserved.
 *
 * @author huyan
 * @date 2017/10/19
 * @desc: 告警的展示类
 */
public class AlertVO extends Alert {

    private String keySearch; // 关键字搜索

    // ----------告警页面的展示字段---------------------
    private String eventSorts;// 与告警相关联的事件名称展示字符
    private String alertLevelInfo; // 告警等级展示字符
    private String alertStateInfo; // 告警状态展示字符
    private String triggerTimeStr; // 告警触发时间展示字符
    private Integer alertCount; // 同类告警数量


    // ----------告警相关的日志数据--------------------
    private String alertData; // 事件数据
    private Map<String, Object> map; // 报表数据

    // ----------页面展示需要的告警名称-----------------
    private String tempAlertName;



    public static AlertVO getAlertVO(Alert alert) {
        if (alert == null)
            return null;
        AlertVO vo = new AlertVO();
        vo.setAlertSid(alert.getAlertSid());
        vo.setAlertName(alert.getAlertName());
        vo.setAlertType(alert.getAlertType());
        vo.setAlertOwner(alert.getAlertOwner());
        vo.setAlertLevel(alert.getAlertLevel());
        vo.setAlertState(alert.getAlertState());
        vo.setTriggerTime(alert.getTriggerTime());
        vo.setCreateTime(alert.getCreateTime());
        vo.setAlertLevelInfo(AlertEnums.AlertLevelEnum.getLevelMsg(alert.getAlertLevel()));
        vo.setAlertStateInfo(AlertEnums.AlertStateEnum.getStateMsg(alert.getAlertState()));
        vo.setTriggerTimeStr(DateFormatUtils.date2Str(alert.getTriggerTime()));
        vo.setR_md5(alert.getR_md5());
        vo.setR_search(alert.getR_search());
        vo.setEarliestTime(alert.getEarliestTime());
        vo.setLatestTime(alert.getLatestTime());
        vo.setAlertResult(alert.getAlertResult());
        vo.setAlertSearch(alert.getAlertSearch());
        vo.setTempAlertName(AlertUtils.getAlertName(alert.getAlertName()));
        return vo;
    }

    public static Alert getAlert(AlertVO vo) {
        if (vo == null) {
            return null;
        }
        Alert alert = new Alert();
        alert.setAlertSid(vo.getAlertSid());
        alert.setAlertName(vo.getAlertName());
        alert.setAlertType(vo.getAlertType());
        alert.setAlertOwner(vo.getAlertOwner());
        alert.setAlertLevel(vo.getAlertLevel());
        alert.setAlertState(vo.getAlertState());
        alert.setTriggerTime(vo.getTriggerTime());
        alert.setCreateTime(vo.getCreateTime());
        alert.setR_md5(vo.getR_md5());
        alert.setR_search(vo.getR_search());
        alert.setEarliestTime(vo.getEarliestTime());
        alert.setLatestTime(vo.getLatestTime());
        alert.setAlertResult(vo.getAlertResult());
        alert.setAlertSearch(vo.getAlertSearch());
        return alert;
    }

    public static List<AlertVO> getAlertVOs(Collection<Alert> list) {
        List<AlertVO> voList = new ArrayList<>();
        for (Alert alert : list) {
            voList.add(getAlertVO(alert));
        }
        return voList;
    }

    private Set<Event> events = new HashSet<>();

    public String getTempAlertName() {
        return tempAlertName;
    }

    public void setTempAlertName(String tempAlertName) {
        this.tempAlertName = tempAlertName;
    }

    public Set<Event> getEvents() {
        return events;
    }

    public void setEvents(Set<Event> events) {
        this.events = events;
    }

    public String getKeySearch() {
        return keySearch;
    }

    public void setKeySearch(String keySearch) {
        this.keySearch = keySearch;
    }

    public Map<String, Object> getMap() {
        return map;
    }

    public void setMap(Map<String, Object> map) {
        this.map = map;
    }

    public String getAlertData() {
        return alertData;
    }

    public void setAlertData(String alertData) {
        this.alertData = alertData;
    }

    public String getEventSorts() {
        Set<Event> events = getEvents();
        if (events != null && events.size() != 0) {
            StringBuilder sb = new StringBuilder();
            for (Event event : events) {
                sb.append(event.getEventSort()).append(",");
            }
            if (sb.length() > 0) {
                return sb.substring(0, sb.length() - 1);
            }
        }
        return "";
    }

    public void setEventSorts(String eventSorts) {
        this.eventSorts = eventSorts;
    }

    public String getAlertLevelInfo() {
        return alertLevelInfo;
    }

    public void setAlertLevelInfo(String alertLevelInfo) {
        this.alertLevelInfo = alertLevelInfo;
    }

    public String getAlertStateInfo() {
        return alertStateInfo;
    }

    public void setAlertStateInfo(String alertStateInfo) {
        this.alertStateInfo = alertStateInfo;
    }

    public String getTriggerTimeStr() {
        return triggerTimeStr;
    }

    public void setTriggerTimeStr(String triggerTimeStr) {
        this.triggerTimeStr = triggerTimeStr;
    }
    public Integer getAlertCount() {
        return alertCount;
    }

    public void setAlertCount(Integer alertCount) {
        this.alertCount = alertCount;
    }
}
